package cn.yunhe.dao.impl;

import cn.yunhe.dao.HouseDao;
import cn.yunhe.model.House;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.sql.SQLException;
import java.util.List;

public class HouseDaoImpl extends HibernateDaoSupport implements HouseDao {
    @Override
    public boolean insertHouse(House house) {
        getHibernateTemplate().save(house);
        return false;
    }

    @Override
    public boolean updateHouse(House house) {
        getHibernateTemplate().update(house);
        return false;
    }

    @Override
    public boolean deleteHouse(int hid) {
        getHibernateTemplate().delete(this.getById(hid));
        return false;
    }

    @Override
    public House getById(int hid) {
        House house=getHibernateTemplate().get(House.class,hid);
        return house;
    }

    @Override
    public List<House> searchHouse(String hql) {
        List<House> houseList= getHibernateTemplate().find(hql);
        return houseList;
    }

    @Override
    public List<House> searchHouse(final House house) {
        List<House> houseList=getHibernateTemplate().executeFind(
                new HibernateCallback<List<House>>() {
                    @Override
                    public List<House> doInHibernate(Session session) throws HibernateException, SQLException {
                        Criteria criteria=session.createCriteria(House.class);

                        if (house!=null){
                            if (house.getHid()>0){
                                criteria.add(Restrictions.eq("hid",house.getHid()));
                            }
                            if (house.getHtitle()!=null&&!house.getHtitle().equals("")){
                                criteria.add(Restrictions.like("htitle",house.getHtitle(),MatchMode.ANYWHERE));
                            }
                            if (house.getHprice()>0){
                                criteria.add(Restrictions.ge("hprice",house.getHprice()));
                            }
                            if (house.getHfloorage()>0){
                                criteria.add(Restrictions.ge("hfloorage",house.getHfloorage()));
                            }
                            if (house.getHpubdate()!=null&&!house.getHpubdate().equals("")){
                                criteria.add(Restrictions.like("hpubdate",house.getHpubdate(),MatchMode.ANYWHERE));
                            }
                        }

                        List<House>houseList111=criteria.list();
                        return houseList111;
                    }
                }
        );
        return houseList;
    }
}



  /*  private int hid;
    private String htitle;//标题
    private double hprice;//价格
    private double hfloorage;//房屋面积
    private String hpubdate;//房产证日期
    private String hcontact;//联系方式
    private String hdesc;//具体描述
    private int hflag;//信息当前是否有效

    private Member member=new Member();
    private Type type=new Type();
    private Street street=new Street();*/